package com.lai.gulimall.auth.controller;

import com.lai.common.constant.AuthServerConstant;
import com.lai.common.exception.BizCodeEnum;
import com.lai.common.utils.R;
import com.lai.common.vo.MemberRespVo;
import com.lai.gulimall.auth.service.LoginService;
import com.lai.gulimall.auth.vo.UserLoginVo;
import com.lai.gulimall.auth.vo.UserRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@Api(value = "登录模块",tags = "登录模块")
public class LoginController {
    @Autowired
    LoginService loginService;


    @PostMapping("/sms/sendVerificationCode")
    @ApiOperation(value = "发送验证码")
    @ApiImplicitParam(name = "phoneNumbers",value = "手机号",required = true,type = "String")
    @ResponseBody
    public R sendVerificationCode(@RequestParam("phoneNumbers") String phoneNumbers,@RequestParam("token")String token) throws Exception {

        boolean flag = loginService.sendVerificationCode(phoneNumbers,token);
        if(flag){
            return R.ok();
        }else{
            return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(),BizCodeEnum.SMS_CODE_EXCEPTION.getMsg());
        }

    }


    //TODO 分布式下的session问题
    @PostMapping("/register")
    @ApiOperation(value = "注册")
    @ApiImplicitParam(name = "UserRegisterVo",value = "注册信息",required = true,paramType = "UserRegisterVo")
    public String register(@Valid UserRegisterVo vo, BindingResult bindResult, Model model, RedirectAttributes redirectAttributes){
        if(bindResult.hasErrors()){
            Map<String, String> collect = bindResult.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            //model.addAttribute("errors",collect);
            redirectAttributes.addFlashAttribute("errors",collect);
            //校验出错
            return "redirect:http://auth.gulimall.com/register.html";
        }
        boolean register = loginService.register(vo);
        if(register){
            //成功
            return "redirect:http://auth.gulimall.com/login.html";
        }else{
            HashMap<String, String> errors = new HashMap<>();
            errors.put("code","验证码错误");
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.gulimall.com/register.html";
        }
    }


    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiImplicitParam(name = "userLoginVo",value = "登录信息",required = true,paramType = "userLoginVo")
    public String login(@Valid UserLoginVo userLoginVo, Model model, HttpSession session,RedirectAttributes redirectAttributes) throws Exception {
        MemberRespVo memberRespVo = loginService.login(userLoginVo);
        if(!StringUtils.isEmpty(memberRespVo)){
            session.setAttribute(AuthServerConstant.LOGIN_USER,memberRespVo);
            return "redirect:http://gulimall.com";
        }else{
            HashMap<String, String> errors = new HashMap<>();
            errors.put("msg",BizCodeEnum.ACCOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.gulimall.com/login.html";
        }

    }
}
